package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	router := gin.Default()

	router.GET("/welcome", Welcome)
	router.POST("/form_post", FormPost)
	router.POST("/post", Post)

	router.Run(":8080")

}

// post get 参数混合获取
func Post(c *gin.Context) {
	message := c.PostForm("message")
	nick := c.DefaultPostForm("nick", "anonymous")
	firstName := c.DefaultQuery("firstName", "ice")
	lastName := c.Query("lastName")

	c.JSON(http.StatusOK, gin.H{
		"message": message,
		"nick": nick,
		"firstName": firstName,
		"lastName": lastName,
	})
}

func FormPost(c *gin.Context) {
	message := c.PostForm("message")
	nick := c.DefaultPostForm("nick", "anonymous")

	c.JSON(http.StatusOK, gin.H{
		"message": message,
		"nick": nick,
	})
}

func Welcome(c *gin.Context){
	firstName := c.DefaultQuery("firstName", "ice")
	lastName := c.Query("lastName")

	c.JSON(http.StatusOK, gin.H{
		"firstName": firstName,
		"lastName": lastName,
	})

}

